/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var store =require("store");
	var activeUser = store.get("user");
	var Form = require("form");
	var Dialog = require("dialog");
    var instance = {
        initComponent: function (params,widget) {
        	var grid = new Grid({
        		parentNode: ".J-grid",
        		model:{
        			url:"api/cate/travelaccount/query",
        			params:function(){
        				var subnav = widget.get("subnav");
        				subnav.hide(["back"]);
        				return{
//        					"attendant.servicePoint.pkServicePoint":subnav.getValue("servicePoint"),
        					"accountType":"Attendant",
        					fetchProperties:"*,attendant.personalInfo.name,attendant.personalInfo.sex,attendant.personalInfo.idNumber,attendant.cno"
        				}
        			},
        			columns:[{
        				name:"attendant.personalInfo.name",
        				label:"员工姓名"
        			},{
        				name:"attendant.personalInfo.sex.value",
        				label:"性别"
        			},{
        				name:"attendant.cno",
        				label:"账号"
        			},{
        				name:"balance",
        				label:"余额"
        			},{
        				name:"operate",
        				label:"操作",
        				format:"button",
        				formatparams:[{
        					id:"edit",
        					text:"充值",
        					handler:function(index,data,rowEle){
        						var remarkForm = new Form({
        							parentNode:".J-dialog-conponent",
        							autoRender:false,
        							model:{
        								defaultButton:false,
        								id:"remarkForm",
        								  items: [{
	        							            name: "balance",
	        							            label: "余额(元)",
	        							            validate : ["required"],
    								                exValidate: function(value) {
    								                	var reg = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
    						                            if (!reg.test(value)) {
    						                                return "请输入正确金额！";
    						                            } else {
    						                                return true;
    						                            }
    						                        }, 
        							            
        								  		},{
        								  			name:"rechargeType",
    												label:"充值方式",
    												type:"select",
    												options:[{
    													key:"Cash",
    													value:"现金"
    												},{
    													key:"WipeCard",
    													value:"刷卡"
    												},{
    													key:"Transfer",
    													value:"转账"
    												}]
        								  		},{
        								  			name:"servicePoint",
    												label:"充值服务点",
    												defaultValue :activeUser.servicePoint.name,
    												 validate : ["required"]
        								  		},{
    												name:"operator",
    												label:"经办人",
    												readonly:true,
    												defaultValue :activeUser.name
    											},{
    												name:"operateDate",
    												label:"充值时间",
    												readonly:true,
    												defaultValue:moment().format("YYYY-MM-DD HH:mm:ss"),
    												 validate : ["required"]
    											}]
        								}	       			            	
        						});
        						widget.set("remarkForm",remarkForm);
    							Dialog.showComponent(remarkForm,{
    								defaultButton:false,
    								title: "充值",
    								setStyle: function(){
										$(".el-dialog-modal .modal").css({
											"top": "10%",
											"width":"50%",
											"left": "25%",
										});
									},
    								buttons :[{
    									id:"save",
    									text:"充值",
     									className:"btn-primary",
    									handler:function(){
    										if(remarkForm.valid()){
    											var FormData = remarkForm.getData();
    											var saveData = {};
    											
//    											saveData.servicePoint = widget.get("subnav").getValue("servicePoint");
    											saveData.balance = FormData.balance;
    											saveData.rechargeType = FormData.rechargeType;
    											saveData.servicePoint=activeUser.servicePoint.pkServicePoint;
    											saveData.operator=activeUser.pkUser;
    											saveData.operateDate = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
    											saveData.travelAccount = data.pkTravelAccount;
    											saveData.version = 0;
    											aw.saveOrUpdate("api/cate/travelaccountflow/saveAttendant",aw.customParam(saveData),function(saveData){
    												Dialog.close();
    							          			widget.get("grid").refresh();
    							       			});
    										};
    									}
    								},{
    									id:"close",
    									text:"取消",
    									handler:function(){
    										Dialog.close();
    									}
    								}]
    							});
        					}
        				},{
        					id:"delete",
        					text:"账户余额",
        					handler:function(index,datas,rowEle){
        						widget.show([".J-Egrid"]).hide([".J-grid"]);
        						widget.get("subnav").hide(["servicePoint","search"]).show(["back"]);
        						aw.ajax({
    								url: "api/cate/travelaccountflow/query",
    								data: {
    									"travelAccount.pkTravelAccount":datas.pkTravelAccount,
    									fetchProperties: "*,operator.name,travelAccount.balance"
    								},
    								success: function(data){
    									widget.get("Egrid").setData(data);
    								}
    							});
        						widget.get("Egrid").refresh;
							}
        				}]
        			},]
        		},
        		
        	});
        	widget.set("grid",grid)
        }
    };
    module.exports = instance;
});
